function mouseButtonDown(hObject, eventdata)
%controls what to do when mouse button is pressed down

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal

% 21-Oct-09 ver. 1.0 (splitted from CISNE.m).
% 23-May-11 ver. 1.1 (updated to work with new UX)

handles=guidata(hObject);



%¿estamos dentro de los ejes?
mouseclick=get(handles.figure1,'SelectionType');
handles.AddSPoint=false; %para probar... nunca vamos a añadir puntos!
if (strcmp(mouseclick,'normal')&~handles.AddSPoint)
    posax=handles.DisplayWidget.getWidgetPosition();
    p=get(gcf,'CurrentPoint');
    %check whether we are inside the axis or not
    if (p(1,1)-posax(1)<posax(3))&&(p(1,1)>posax(1))&&...
            (p(1,2)-posax(2)<posax(4))&&(p(1,2)>posax(2))
        p=handles.DisplayWidget.currentPoint;
        [xlim ylim]=handles.DisplayWidget.getAxisLim();
        %check that we are inside the plotted image, even if axis object is
        %bigger.
        if (p(1,1)<xlim(2)&&p(1,1)>xlim(1))&&(p(1,2)<ylim(2)&&p(1,2)>ylim(1))
            if handles.DisplayWidget.isAction('new')
                handles.xp0=p(1,1);
                handles.yp0=p(1,2);
                handles.mouseButtonDown=1;
                handles.gridAction='new';
                handles.DisplayWidget.resetTempRect();
                handles.DisplayWidget.setTempRect(p(1,1),p(1,2));
            
            elseif handles.DisplayWidget.isAction('addCPoint')
                handles.xp0=p(1,1);
                handles.yp0=p(1,2);
                handles.mouseButtonDown=1;
                handles.gridAction='addCPoint';
                
            elseif handles.DisplayWidget.isAction('select')
                %select grid using Tag of gco.
                tagName=get(gco,'Tag');
                tag=regexp(tagName,'-','split');%split and get Name.
                tag=tag{1};
                handles.TreeWidget.selectGrid(tag);
                handles.DisplayWidget.setAction('');
            elseif handles.DisplayWidget.isAction('edit')
                %determine whether we need to move or resize a comp grid.
                tagName=get(gco,'Tag');
                tag=regexp(tagName,'-','split');%split and get Name.
                tag=tag{1};
                if handles.DisplayWidget.isActiveElement(tag)
                if handles.DisplayWidget.isCGrid(tag) %si es una malla
                    handles.xp0=p(1,1);
                    handles.yp0=p(1,2);
                    handles.mouseButtonDown=1;
                    handles.DisplayWidget.resetTempRect();
                    handles.DisplayWidget.setTempRect(p(1,1),p(1,2));
                    %handles.TreeWidget.selectGrid(tag);
                    
                    if isempty(strfind(tagName,'Corners'))
                        %handles.DisplayWidget.setActiveGrid(tag);
                        handles.gridAction='move';
                        setptr(handles.figure1,'closedhand');
                        %set(handles.figure1,'Pointer','fleur');
                    else
                        %get corner objects.
                        pObj=handles.DisplayWidget.getPointObjs(tag);
                        pointfields=sort(fieldnames(pObj));
                        %get corner coordinates
                        points=handles.DisplayWidget.getPointCoord(tag);
                        oposcorner=[3 4 1 2];
                        pointer={'botl' 'topr' 'topl' 'botr'};
                        handles.gridAction='resize';
                        for k=1:length(pointfields)
                            if(gco==pObj.(pointfields{k}))
                                handles.fixcorner=points(:,oposcorner(k));
                                set(handles.figure1,'Pointer',pointer{k});
                            end
                        end
                    end
                elseif handles.DisplayWidget.isCPoint(tag)
                    handles.mouseButtonDown=1;
                    %handles.TreeWidget.selectGrid(tag);
                    handles.DisplayWidget.resetTempPoint();
                    handles.DisplayWidget.setTempPoint(p(1,1),p(1,2));
                    handles.gridAction='movePoint';
                    setptr(handles.figure1,'closedhand');
                    %handles.DisplayWidget.setActiveGrid(tag);
                    
                end
                end
            elseif handles.DisplayWidget.isAction('zoomIn')
                handles.DisplayWidget.zoomIn;
                handles.DisplayWidget.setAction('');
            elseif handles.DisplayWidget.isAction('zoomOut')
                handles.DisplayWidget.zoomOut;
                handles.DisplayWidget.setAction('');
            elseif handles.DisplayWidget.isAction('zoomArea')
                handles.xp0=p(1,1);
                handles.yp0=p(1,2);
                handles.mouseButtonDown=1;
                handles.gridAction='zoomArea';
                handles.DisplayWidget.resetTempRect();
                handles.DisplayWidget.setTempRect(p(1,1),p(1,2));
            end
        end
    end
elseif (strcmp(mouseclick,'normal')&handles.AddSPoint)
    posax=get(handles.ax_bathym,'Position');
    p=get(gcf,'CurrentPoint');
    %check whether we are inside the axis or not
    if (p(1,1)-posax(1)<posax(3))&&(p(1,1)>posax(1))&&(p(1,2)-posax(2)<posax(4))&&(p(1,2)>posax(2))
        p=get(gca,'CurrentPoint');
        A=num2cell(p(1,1:2));
        Points=get(handles.TW_Points,'Data');
        if ~isempty(Points)
            Points(end+1:end+size(A,1),:)=A;
        else
            Points=A;
        end
        set(handles.TW_Points,'Data',Points);
        if isfield(handles,'pbat');
            %plot those points in the bathymetry.
            plotControlPoints(handles);
        end
        handles.AddSPoint=false;
    end
    
end
guidata(hObject,handles);



